<?php
define('APPGESTAPP','1.00');
/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

//carica la lingua se esiste
$lang=lang();
switch($lang){
    case "en":
	define("_ADMIN_PREFERENCE","ADMIN PREFERENCE");
	define("_CHANGE_PREFERENCES","change preferences");
	define("_CHANGE_PASSWORD","Change Password");
	define("_RETYPE_PASSWORD","Retype Password");
	define("_SET_PASSWORD","change password");
	define("_ADMINISTRATION_PRIVILEGE_REQUESTED", "administration privilege requested");
	define("_CHANGE","Change");
	define("_CONTINUA","Continue");
	break;
    default;
	define("_ADMIN_PREFERENCE","ADMIN PREFERENCE");
	define("_CHANGE_PREFERENCES","change preferences");
	define("_CHANGE_PASSWORD","Change Password");
	define("_SET_PASSWORD","change password");
	define("_RETYPE_PASSWORD","Retype Password");
	define("_ADMINISTRATION_PRIVILEGE_REQUESTED", "administration privilege requested");
	define("_CHANGE","Change");
	define("_CONTINUA","Continua");
	break;
}


function makeform($randpath,$nf,$appname){
    echo "<h3>$nf</h3>\n";


// rinomina -------------------------------------------------------------------------------------------------------------------------------
    echo "<form name='$nf' action='index.php?appname=$appname&name=$nf&op=change' method='post'>\n";

    echo "<input type='hidden' name='app-path' value='$randpath' >\n";
    echo "percorso<input type='text' name='app-newpath' value='$randpath' ><br/>\n";

    if ($nf[0]=='_')
	echo "<input name='hidden' type='checkbox' checked=1 >nascosto\n";
    else
	echo "<input name='hidden' type='checkbox' >nascosto\n";

    echo "<select name='order'>\n";
    if ($nf[2]=='_' )
	echo "<option>".substr($nf,0,2)."</option>\n";
    echo "<option> </option>\n";
    for($i='0';$i<'09';$i++)
	echo "<option>".substr('0'.$i,-2)."</option>\n";
    echo "</select>\n";


    if ($nf[0]=='_' ){
	echo "<input type=text name='application' value='".substr($nf,1)."'/>\n";
    }
    else{
	if ($nf[2]=='_' ){
	    echo "<input type=text name='application' value='".substr($nf,3)."'/>\n";
	}
	else{
	    echo "<input type=text name='application' value='$nf'/>\n";
	}
    }
    echo "<input type='submit' value='change'>\n";
    echo "</form>\n";
// ----------------------------------------------------------------------------------------------------------------------------------------
    echo "<hr/>\n";
// elimina -------------------------------------------------------------------------------------------------------------------------------
    echo "<input type='button' value='del application $nf' onclick=\"javascript: if (confirm('remove $nf')==true){location='index.php?appname=$appname&name=$nf&app-path=$randpath&op=del';}\" />\n";
// ----------------------------------------------------------------------------------------------------------------------------------------
    echo "<hr/>\n";
// new -------------------------------------------------------------------------------------------------------------------------------

    echo "<h3>add application</h3>";
    //$lista=glob("applications/$appname/template/*");
    $fd=opendir("applications/$appname/template/");
    while (false !== ($nf= readdir($fd))){
	if ($nf[0]!='.' && $nf[0]!='_'  )
	$lista[$i++]=$nf;
    }
    closedir($fd);
    sort($lista);

    echo "<form name='newapp' action='index.php?appname=$appname&app-path=$randpath&name=$nf&op=install' method='post'>\n";
    echo "<select name='addnew'>\n";
    foreach($lista as $elemento)
	echo "<option value='$elemento'>".basename($elemento)."</option>\n";
    echo "</select>\n";
    echo "<input type='submit' value='install'>\n";
    echo "</form>\n";
// ----------------------------------------------------------------------------------------------------------------------------------------
}


global $tree,$tree_i;
$tree_i=1;

function dirconfig($randpath,$myparent,$appname){
    global $tree,$tree_i;
    //*
    $fdh=opendir($randpath);
    while (false !== ($nf= readdir($fdh))){
	if ($nf[0]!='.' )
	$fd[$i++]=$nf;
    }
    closedir($fdh);
    //*/
    //$fd=glob($randpath."*");


    if (count($fd)){
	sort($fd);
	foreach($fd as $nf){
	    $nf=basename($nf);
	    if ($nf[0]!='.' ){
		if (is_dir($randpath.$nf)){
		    if (file_exists($randpath.$nf."/application.inc")){
			$tree[$tree_i++]=$nf;
			echo "<script>tree.insertNewItem($myparent,$tree_i,\"$nf\",0,0,0,0,\"SELECT\");</script>\n ";
			echo"<div id='$tree_i' style=\"display:none;border:2;\" >\n";
			makeform($randpath,$nf,$appname);
			echo "</div>\n";
			dirconfig($randpath.$nf."/",$tree_i,$appname);
		    }
		}
		else{
		    if ($nf!="application.inc" && $nf!="upgrade.inc" && substr($nf,-3)=="inc"){
			$tree[$tree_i++]=$nf;
			echo "<script>tree.insertNewItem($myparent,$tree_i,\"$nf\",0,0,0,0,\"SELECT\");</script>\n ";
			echo "<div id='$tree_i' style=\"display:none;border:2;\" >\n";
			makeform($randpath,$nf,$appname);
			echo "</div>\n";
		    }
		}
	    }
	}
    }
echo "<script>tree.closeAllItems(0);</script>\n";
}








?>
<?php if (admin_isadmin()) {
        admin_writelog(_ADMIN_PREFERENCE);
?>
    <p>


<style>
.fold
{
	border: 1px solid #808080;

}

</style>

<style>
	body {font-size:12px}
	.{font-family:arial;font-size:12px}
	h1 {cursor:hand;font-size:16px;margin-left:10px;line-height:10px}
	xmp {color:green;font-size:12px;margin:0px;font-family:courier;background-color:#e6e6fa;padding:2px}
</style>


<h3>Gest App</h3>
	<link rel="STYLESHEET" type="text/css" href="../css/dhtmlXTree.css">
	<script  src="js/dhtmlxTree/js/dhtmlXCommon.js"></script>
	<script  src="js/dhtmlxTree/js/dhtmlXTree.js"></script>

	<table border='1'>
		<tr>
			<td valign="top" >
                <div id="treeboxbox_tree" style="width: 250; height: 480;background-color:#f5f5f5;border :1px solid Silver;; overflow:auto;"></div>
			</td>
			<td valign="top" width="100%">



</p>
	<script>
			tree=new dhtmlXTreeObject("treeboxbox_tree","100%","100%",0);
			tree.setImagePath("js/dhtmlxTree/imgs/");

            function tonclick(id){
                var ob;
                for (i=0;i<199;i++){
                    ob=document.getElementById(i);
                    if (ob)ob.style.display="none";
                }

                ob=document.getElementById(id);
                if (ob)ob.style.display="inline";
                //alert (id);
            }
	       tree.setOnClickHandler(tonclick);

	</script>

<?php
	$op=$_GET['op'];

	if ($op==""){
	    //$lista=glob("applications/$appname/template/*");
	    $fd=opendir("applications/$appname/template/");
	    while (false !== ($nf= readdir($fd))){
		if ($nf[0]!='.' && $nf[0]!='_'  )
		    $lista[$i++]=$nf;
	    }
	    closedir($fd);
	    sort($lista);
	    echo "<script>tree.insertNewItem(0,1,\"applications\",0,0,0,0,\"SELECT\");</script>\n ";
	    echo "<div id='1' style=\"display:none;border:2;\" >\n";
	    echo "<h3 >add application</h3>";
	    echo "<form name='newapp' action='index.php?appname=$appname&app-path=applications&name=&op=install' method='post'>\n";
	    echo "<select name='addnew'>\n";
	    foreach($lista as $elemento)
		echo "<option value='$elemento'>".basename($elemento)."</option>\n";
	    echo "</select>\n";
	    echo "<input type='submit' value='install'>\n";
	    echo "</form>\n";
	    echo "</div>\n";

	    dirconfig("applications/",1,$appname);
	    echo "<script>tonclick(0);</script>";
	}

	if ($op=="change"){
	    $name=$_GET['name'];
	    $app_path=$_POST['app-path'];
	    $app_newpath=$_POST['app-newpath'];

	    $hidden=$_POST['hidden'];
	    $order=$_POST['order'];
	    $application=$_POST['application'];

	    echo "OP:$op<br/>\n\n";
	    echo "path:$app_path<br/>\n";
	    echo "newpath:$app_newpath<br/>\n";
	    echo "applicazione:$application<br/>\n";
	    echo "nascosto:$hidden<br/>\n";
	    echo "ordine:$order<br/>\n";
	    echo "file:$name<br/>\n";

	    if ($hidden==''){
		if ($order==''){
		    $newname=$application;
		}
		else{
		    $newname=$order."_".$application;
		}

	    }
	    else{
		if ($order==''){
		    $newname="_".$application;
		}
		else{
		    $newname="_".$order."_".$application;
		}
	    }
	    echo "nuovo nome:$newname<br/>\n";
	    rename("$app_path/$name","$app_newpath/$newname");
	    echo "<h3 class='edit'><a href='index.php?appname=$container&subapp=$subapp'>"._CONTINUA."</a></h3>";

	}

	if ($op=="del"){
	    $name=$_GET['name'];
	    $app_path=$_GET['app-path'];
	    echo "".$app_path.$name." eliminata";
	    deltree("".$app_path.$name);
	    echo "<h3 class='edit'><a href='index.php?appname=$container&subapp=$subapp'>"._CONTINUA."</a></h3>";
	}

	if ($op=="install"){
	    $name=$_GET['name'];
	    $app_path=$_GET['app-path'];
	    $addnew=$_POST['addnew'];
	    echo "applicazione $app_path.$name/".basename($addnew)." installata<br/>" ;
	    if (is_dir($addnew))
		copytree("$addnew",$app_path.$name."/".basename($addnew));
	    else
		copy("$addnew",$app_path.$name."/".basename($addnew));

	    echo "<h3 class='edit'><a href='index.php?appname=$container&subapp=$subapp'>"._CONTINUA."</a></h3>";
	}

?>

	    </td>
	</tr>
    </table>
    <?php
    }else{
    ?>
	<p><?=_ADMINISTRATION_PRIVILEGE_REQUESTED?> </p>
    <?php } ?>
<hr/>

